package com.kindergarten.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kindergarten.pojo.Notice;
import com.kindergarten.service.NoticeService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.MMGridPageVoBean;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("notice-api")
public class NoticeController {

    @Autowired
    NoticeService noticeService;


    /**
     * 01-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("topage")
    public String topage() {
        return "notice";  //模板引擎页面
    }


    /**
     * 02-跳转到模板引擎页面
     * @return
     */
    @GetMapping("toaddpage")
    public String toaddpage() {
        return "noticeAdd";  //模板引擎页面
    }

    /**
     * 02-跳转到模板引擎页面
     * @return
     */
    @GetMapping("toupdatepage")
    public String toupdatepage(Integer id, Model model) {
        //数据回显用的
        Notice notice = noticeService.getById(id);
        model.addAttribute("noticeObject",notice);
        return "noticeUpdate";  //模板引擎页面
    }

    /**
     * 02-数据加载分页
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    public MMGridPageVoBean page(Notice notice) {

        MMGridPageVoBean pageVoBean = new MMGridPageVoBean();
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("del",0); //删除不查询
        queryWrapper.ge(notice.getStartTime() != null && !"".equals(notice.getStartTime()), "create_time", notice.getStartTime());
        queryWrapper.le(notice.getEndTime() != null && !"".equals(notice.getEndTime()), "create_time", notice.getEndTime());
        IPage<Notice> page = noticeService.page(new Page<Notice>(notice.getPage(), notice.getLimit()), queryWrapper);
        pageVoBean.setRows(page.getRecords()); //每页数据
        pageVoBean.setTotal(page.getTotal()); //总条数
        return pageVoBean;
    }

    /**
     * 新增
     * @return
     */
    @ResponseBody
    @PostMapping("addNotice")
    public DataResults addNotice(Notice notice){
        return DataResults.success(ResultCode.SUCCESS,noticeService.save(notice));
    }

    /**
     * 更新
     * @return
     */
    @ResponseBody
    @PutMapping("updateNotice")
    public DataResults updateNotice(Notice notice){
        return DataResults.success(ResultCode.SUCCESS,noticeService.updateById(notice));
    }

    /**
     * 删除
     * @return
     */
    @ResponseBody
    @DeleteMapping("delNotice/{id}")
    public DataResults delNotice(@PathVariable("id") Integer id){
        Notice notice = new Notice();
        notice.setId(id);
        notice.setDel(1);
        return DataResults.success(ResultCode.SUCCESS,noticeService.updateById(notice));
    }
}
